﻿$(document.body).keydown(function (e) {
    if (e.keyCode == 13 && $("#loginBtn").attr("disabled") != "disabled") {
        submitform();
    }
});


$(window).resize(function () {          //当浏览器大小变化时修改左端的margin-left
    setting();
});


var submitform = function () {
    var username = $("#username").val();
    var password = $("#password").val();
    var verifyCode = $("#verifyCode").val();
    var sessionVrcode = sessionStorage.getItem("session_vrCode");
    $("#loginCycle").addClass("loginCycle");
    $.ajax({
        type: "POST",
        url: "/login",
        data: {
            "id": username,
            "password": password,
            "verifyCode": verifyCode
        },
        success: function (data) {
            console.log(data);
            if (data.code == 201) {
                // message("登陆成功", "","success", 500);
                setTimeout(function () {
                    var token = data.data.token;
                    localStorage['token'] = token;
                    localStorage['username'] = data.data.name;
                    localStorage['role'] = data.data.role.name;
                    localStorage['id'] = data.data.id;
                    crossMessaeg("登陆成功", "success");
                    postPage("/home", "token", token);
                }, 1000);
            } else {
                $("#loginCycle").removeClass("loginCycle");
                $("#password").val("");
                $("#verifyCode").val("");
                $("#loginBtn").attr("disabled", "disabled");
                $("#vrCodeImg").attr("src", "/verifyCode?time=" + new Date().getTime());
                swal(data.msg, "请重新尝试", "error");
            }
        }
    })
};


$("#password").focus(function () {
    $(this).attr("type", "password");
    $(this).attr("autocomplete", "new-password");
});

$("#vrCodeImg").click(function () {

    this.src = "/verifyCode?time=" + new Date().getTime();
});


$("#username,#password,#verifyCode").focus(function () {
    $(this).parent().addClass("enter");


});
//账号,密码,验证码的输入框每次聚焦都会调用这个事件函数
$("#username,#password,#verifyCode").focusout(function () {
    //账号,密码,验证码input框内的值value不为null且不等于""时表示对应的input框有值
    if ($("#username").val() != null && $("#username").val() != ""
        && $("#password").val() != null && $("#password").val() != ""
        && $("#verifyCode").val() != null && $("#verifyCode").val() != "") {
        //去掉登录按钮不可点击的属性和样式
        $("#loginBtn").addClass("canbutton");
        $("#loginBtn").removeAttr("disabled");
    } else {
        //否则说明其中有输入框未输入,将登录按钮属性改为不可点击和增加不可点击的样式
        $("#loginBtn").removeClass("canbutton");
        $("#loginBtn").attr("disabled","disabled")
    }
    // $(this).parent().removeClass("enter");
});


$("#username,#password,#verifyCode").keydown(function () {
    if ($("#username").val() != null && $("#username").val() != ""
        && $("#password").val() != null && $("#password").val() != ""
        && $("#verifyCode").val() != null && $("#verifyCode").val() != "") {
        $("#loginBtn").addClass("canbutton");
        $("#loginBtn").removeAttr("disabled");
    }
});

// $("#username,#password,#verifyCode").blur(function () {
//     if($(this).children("input").val()!= ""){
//         $(this).addClass("success_enter");
//     }
// });

$(".dialog").css({
        "width": $(window).width(), "height": $(window).height()
    }
);

var setting = function () {
    var window_width = $(window).width();
    var login_width = $("#login").width();
    var margin_left = (window_width - login_width) / 2;
    $("#login").css({"margin-left": margin_left});
    $(document.body).height($(window).height());
};


